package jdbc;
/**
 * 将注册，登录，修改信息改为使用预编译SQL完成
 */

import java.sql.*;
import java.util.Scanner;

public class Homework {
    /*public static void main(String[] args) throws Exception{
        Connection connection = DBUtil.getConnection();
        Statement statement = connection.createStatement();
        String sql = "CREATE TABLE user1 ( \n" +
                     "id INT PRIMARY KEY AUTO_INCREMENT, \n" +
                     "username VARCHAR(32), \n" +
                     "password VARCHAR(32), \n" +
                     "nickname VARCHAR(32), \n" +
                     "age INT(3) \n" +
                     ")";
        System.out.println(sql);
        statement.execute(sql);
        System.out.println("执行成功");
        connection.close();
        }
        */  //创建表user1

    /* public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection();) {
            Statement statement = connection.createStatement();
            String sql = "INSERT INTO user1(username,password,nickname,age) " +
                         "VALUES('曹操','123456','阿满',30), " +
                         "('关羽','666666','云长',28), " +
                         "('赵云','778899','子龙',18)";
            int num = statement.executeUpdate(sql);
            System.out.println("插入完毕，影响了表中"+num+"条记录");
        } catch (SQLException e) {
            e.getStackTrace();
        }
    }*/  //插入3条数据

    /*public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("欢迎注册");
        System.out.println("请输入用户名:");
        String username = scan.nextLine();
        System.out.println("请输入密码:");
        String password = scan.nextLine();
        System.out.println("请输入昵称:");
        String nickname = scan.nextLine();
        System.out.println("请输入年龄:");
        int age = Integer.parseInt(scan.nextLine());
        System.out.println(username+","+password+","+nickname+","+age);
        try (Connection connection = DBUtil.getConnection();) {
            String sql = "INSERT INTO user1(username,password,nickname,age) "+
                         "VALUES(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ps.setString(3,nickname);
            ps.setInt(4,age);
            int num = ps.executeUpdate();
            System.out.println(num>0?"注册成功":"注册失败");
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }*/  //注册预编译SQL

    /*public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("欢迎登录");
        System.out.println("请输入用户名:");
        String username = scan.nextLine();
        System.out.println("请输入密码:");
        String password = scan.nextLine();
        try (Connection connection = DBUtil.getConnection();) {
            String sql = "SELECT username,password "+
                         "FROM user1 "+
                         "WHERE username=? AND password=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                System.out.println("登录成功");
            } else {
                System.out.println("登录失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }*/  //登录预编译SQL

    /*public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请谨慎修改注册信息");
        System.out.println("请输入新的用户名:");
        String username = scan.nextLine();
        System.out.println("请输入新的密码:");
        String password = scan.nextLine();
        System.out.println("请输入新的昵称:");
        String nickname = scan.nextLine();
        System.out.println("请输入新的年龄:");
        int age = Integer.parseInt(scan.nextLine());
        try (Connection connection = DBUtil.getConnection();) {
            String sql = "UPDATE user1 "+
                         "SET username=?,password=?,nickname=?,age=? "+
                         "WHERE id=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ps.setString(3,nickname);
            ps.setInt(4,age);
            ps.setInt(5,4);
            int num = ps.executeUpdate();
            System.out.println(num>0?"修改成功":"修改失败");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }*/  //修改预编译SQL

}







